export interface MlStorageInterface {
  setItem(_key: string, _value: any): void
  setItems(_data: { key: string; value: any }[]): void
  getItem(_key: string): string | null
  removeItem(_key: string): void
  removeItems(_keys: string[]): void
  clear(): void
  has(_key: string): boolean
}

export class MlStorage implements MlStorageInterface {
  static PREFIX = import.meta.env.VITE_STORAGE_PRE
  storage: Storage

  constructor(storage: Storage) {
    this.storage = storage
  }

  setItem(key: string, value: any) {
    this.storage.setItem(`${MlStorage.PREFIX}${key}`, value)
  }
  setItems(data: { key: string; value: any }[]): void {
    data.forEach(item => this.setItem(item.key, item.value))
  }

  getItem(key: string) {
    return this.storage.getItem(`${MlStorage.PREFIX}${key}`)
  }

  removeItem(key: string) {
    this.storage.removeItem(`${MlStorage.PREFIX}${key}`)
  }
  removeItems(keys: string[]) {
    keys.forEach(key => this.removeItem(key))
  }

  clear() {
    this.storage.clear()
  }

  has(key: string) {
    return this.storage.getItem(`${MlStorage.PREFIX}${key}`) !== null
  }
}
